package com.sx.first.action;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.sx.first.biz.ApplystorageBiz;
import com.sx.first.biz.EmployeeBiz;
import com.sx.first.biz.MaterialBiz;
import com.sx.first.biz.ProductBiz;
import com.sx.first.biz.StoragetypeBiz;
import com.sx.first.biz.WarehouseBiz;
import com.sx.first.model.Applystorage;
import com.sx.first.model.Employee;
import com.sx.first.model.Material;
import com.sx.first.model.Product;
import com.sx.first.model.Storagetype;
import com.sx.first.model.Warehouse;

@Controller
@Scope("prototype")
@Namespace("/applystorage")
@Results({ @Result(name = "list", location = "list.jsp"), @Result(name = "add", location = "add.jsp"),
		@Result(name = "update", location = "update.jsp"), @Result(name = "emplist", location = "emp.jsp") })
public class ApplystorageAction extends BaseAction {
	private Employee emp;
	private Applystorage app;
	private List<Applystorage> appList;
	private List<Employee> empList;
	private List<Product> proList;
	private List<Material> matList;
	private List<Storagetype> stoList;
	private List<Warehouse> warList;
	private ApplystorageBiz applystorageBiz;
	private ProductBiz productBiz;
	private EmployeeBiz employeeBiz;
	private MaterialBiz materialBiz;
	private StoragetypeBiz storagetypeBiz;
	private WarehouseBiz warehouseBiz;

	@Action(value = "emplist")
	public String emplist() {
		appList = applystorageBiz.findApply();
		return "emplist";
	}

	@Action(value = "list")
	public String list() {
		appList = applystorageBiz.findAll(app.getEmp().getEmpId());
		emp = employeeBiz.findById(app.getEmp().getEmpId());
		return "list";
	}

	@Action(value = "add")
	public String add() {
		empList = employeeBiz.findAll();
		proList = productBiz.findAll();
		matList = materialBiz.findAll();
		stoList = storagetypeBiz.findAll();
		warList = warehouseBiz.findAll();
		return "add";
	}

	@Action(value = "doAdd")
	public String doAdd() {
		applystorageBiz.save(app);
		writeJson();
		return null;
	}

	@Action(value = "delete")
	public String delete() {
		applystorageBiz.delete(app.getAppId());
		writeJson();
		return null;
	}

	@Action(value = "update")
	public String update() {
		app = applystorageBiz.findById(app.getAppId());
		empList = employeeBiz.findAll();
		proList = productBiz.findAll();
		matList = materialBiz.findAll();
		stoList = storagetypeBiz.findAll();
		warList = warehouseBiz.findAll();
		return "update";
	}

	@Action(value = "doUpdate")
	public String doUpdate() {
		applystorageBiz.update(app);
		writeJson();
		return null;
	}

	@Action(value = "findProductByMatId")
	public void findProductByMatId() {
		HttpServletResponse response = ServletActionContext.getResponse();
		String str = ServletActionContext.getRequest().getParameter("matId");
		int matId = Integer.parseInt(str);
		proList = productBiz.findAllByMatId(matId);
		for (int i = 0; i < proList.size(); i++) {
			proList.get(i).setMat(null);
		}
		String jsonStr = new Gson().toJson(proList);
		writeJsonString(response, jsonStr);
	}

	/**********************/

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public Applystorage getApp() {
		return app;
	}

	public void setApp(Applystorage app) {
		this.app = app;
	}

	public List<Applystorage> getAppList() {
		return appList;
	}

	public void setAppList(List<Applystorage> appList) {
		this.appList = appList;
	}

	public List<Employee> getEmpList() {
		return empList;
	}

	public void setEmpList(List<Employee> empList) {
		this.empList = empList;
	}

	public List<Product> getProList() {
		return proList;
	}

	public void setProList(List<Product> proList) {
		this.proList = proList;
	}

	public List<Material> getMatList() {
		return matList;
	}

	public void setMatList(List<Material> matList) {
		this.matList = matList;
	}

	public List<Storagetype> getStoList() {
		return stoList;
	}

	public void setStoList(List<Storagetype> stoList) {
		this.stoList = stoList;
	}

	public List<Warehouse> getWarList() {
		return warList;
	}

	public void setWarList(List<Warehouse> warList) {
		this.warList = warList;
	}

	public void setApplystorageBiz(ApplystorageBiz applystorageBiz) {
		this.applystorageBiz = applystorageBiz;
	}

	public void setProductBiz(ProductBiz productBiz) {
		this.productBiz = productBiz;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}

	public void setMaterialBiz(MaterialBiz materialBiz) {
		this.materialBiz = materialBiz;
	}

	public void setStoragetypeBiz(StoragetypeBiz storagetypeBiz) {
		this.storagetypeBiz = storagetypeBiz;
	}

	public void setWarehouseBiz(WarehouseBiz warehouseBiz) {
		this.warehouseBiz = warehouseBiz;
	}

}
